/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;
import java.util.*;
import java.util.regex.*;

/**
 *
 * @author Dung Doan
 */
public class OrderByType {
    private String m_attribute;
    private String m_table;
    private Boolean m_isDesc;

    public OrderByType(String attribute, String table, Boolean isDesc)
    {
        m_attribute = attribute;
        m_isDesc = isDesc;
        m_table = table;
    }
    public OrderByType()
    {
        m_attribute = null;
        m_table = null;
        m_isDesc = false;
    }
    
    public OrderByType(String root)
    {
        String query = root.toUpperCase();
        int index = query.indexOf("DESC");
        if (index >= 0)
        {
            m_isDesc = true;
            String substr = root.substring(0, index).trim();
            String REGEX = "\\.";
            Pattern p = Pattern.compile(REGEX);
            String[] items = p.split(substr);
            if (items.length == 2)
            {
                m_table = items[0];
                m_attribute = items[1];   
            }
            else
            {
                m_attribute = substr;
                m_table = null;
            }
            
            return;
        }
        
        index = query.indexOf("ASC");
        m_isDesc = false;
        String substr;
        if (index >= 0)
            substr = root.substring(0, index).trim();
        else
            substr = root.trim();
        String REGEX = "\\.";
        Pattern p = Pattern.compile(REGEX);
        String[] items = p.split(substr);
        if (items.length == 2)
        {
            m_table = items[0];
            m_attribute = items[1];   
        }
        else
        {
            m_attribute = substr;
            m_table = null;
        }
        return;
        
        
       
    }
    public Boolean isValid()
    {
        if (m_attribute == null)
            return false;
        return true;
    }
    /**
     * @return the m_attribute
     */
    public String getM_attribute() {
        return m_attribute;
    }

    /**
     * @param m_attribute the m_attribute to set
     */
    public void setM_attribute(String m_attribute) {
        this.m_attribute = m_attribute;
    }

    /**
     * @return the m_isDesc
     */
    public Boolean getM_isDesc() {
        return m_isDesc;
    }

    /**
     * @param m_isDesc the m_isDesc to set
     */
    public void setM_isDesc(Boolean m_isDesc) {
        this.m_isDesc = m_isDesc;
    }

    /**
     * @return the m_table
     */
    public String getM_table() {
        return m_table;
    }

    /**
     * @param m_table the m_table to set
     */
    public void setM_table(String m_table) {
        this.m_table = m_table;
    }
    
}
